package org.jeecg.modules.ParameterModule.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.ParameterModule.entity.*;
import org.jeecg.modules.ParameterModule.mapper.CategoryInfoMapper;
import org.jeecg.modules.ParameterModule.mapper.ImportExportMapper;
import org.jeecg.modules.ParameterModule.service.CategoryInfoService;
import org.jeecg.modules.ParameterModule.service.ImportExportService;
import org.jeecg.modules.zhouAdmin.admin.fabeicCenter.pojo.MySysUser;
import org.jeecg.modules.zhoupackage.pojo.MlccFabricInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Random;
import java.util.UUID;

/**
 * 导入导出业务逻辑处理层
 * */
@Service
public class ImportExportServiceImpl extends ServiceImpl<ImportExportMapper, ExcelCategoryInfo> implements ImportExportService {
    @Autowired
    private ImportExportMapper importMapperMapper;
    //测试品类
    @Autowired
    private CategoryInfoMapper logicMapper;

    /**
     * 导出定制项下的参数
     */
    @Override
    public List<ExcelCategoryInfo> ExportCategory(String category_id) {
        List<ExcelCategoryInfo> datalist = importMapperMapper.ExportCategory(category_id);
        return datalist;
    }
    /**
     * 导出绣字工艺-绣字部位
     */
    @Override
    public List<ExcelXiuziLocaltion> Exportxiuzi_localtion(String category_id) {
        List<ExcelXiuziLocaltion> datalist = importMapperMapper.Exportxiuzi_localtion(category_id);
        return datalist;
    }
    /**
     * 导出绣字工艺-绣字颜色
     */
    @Override
    public List<ExcelXiuziColor> Exportxiuzi_color(String category_id) {
        List<ExcelXiuziColor> datalist = importMapperMapper.Exportxiuzi_color(category_id);
        return datalist;
    }
    /**
     * 导出绣字工艺-绣字图片
     */
    @Override
    public List<ExcelXiuziFontImg2> Exportxiuzi_img(String category_id) {
        List<ExcelXiuziFontImg2> datalist = importMapperMapper.Exportxiuzi_img(category_id);
        return datalist;
    }
    /**
     * 导出绣字工艺-绣字字体
     */
    @Override
    public List<ExcelXiuziFontImg> Exportxiuzi_font(String category_id) {
        List<ExcelXiuziFontImg> datalist = importMapperMapper.Exportxiuzi_font(category_id);
        return datalist;
    }
    /**
     * 导出绣字工艺-绣字高度
     */
    @Override
    public List<ExcelXiuziHeight> Exportxiuzi_height(String category_id) {
        List<ExcelXiuziHeight> datalist = importMapperMapper.Exportxiuzi_height(category_id);
        return datalist;
    }
    /**
     * 导出尺寸
     * @param type   0成衣尺寸，1净体尺寸
     * @param category_id 类别id
     */
    @Override
    public List<ExcelBodysizes> Export_bodysizes(String category_id, int type) {
        List<ExcelBodysizes> datalist = importMapperMapper.Export_bodysizes(category_id,type);
        return datalist;
    }
    /**
     * 导出返修尺寸
     * @param category_id 类别id
     */
    @Override
    public List<ExcelFanxiuBodysize> Export_fanxiu_bodysizes(String category_id) {
        List<ExcelFanxiuBodysize> datalist = importMapperMapper.Export_fanxiu_bodysizes(category_id);
        return datalist;
    }
    /**
     * 导出工艺
     * @param category_id 类别id
     */
    @Override
    public List<ExcelCategoryGongyi> Export_all_gongyi(String category_id) {
        List<ExcelCategoryGongyi> datalist = importMapperMapper.Export_all_gongyi(category_id);
        return datalist;
    }
    /**
     * 导出撞色项
     * @param category_id 类别idID
     */
    @Override
    public List<ExcelZhuangseInfo> Export_category_zhuangse(String category_id) {
        List<ExcelZhuangseInfo> datalist = importMapperMapper.Export_category_zhuangse(category_id);
        return datalist;
    }

    /**
     * 品类下面导入定制项数据
     * @param list    转换的实体类
     * @param category_id 面料册ID
     */
    @Override
    public int importCategoryExcel(List<ExcelCategoryInfo> list, String category_id) {
        try {
            if (list == null || list.size() <= 0) {
                return 1;
            } else {
                //获取登录用户信息
                LoginUser loginuser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
                for (int i = 0; i < list.size(); i++) {
                    if(list.get(i).getParentName()!=null && !"".equals(list.get(i).getParentName()) && !"".equals(list.get(i).getParentName().trim())){
                        //根据品类id和定制项name查询定制项
                        ExcelCategoryInfo dingzhi_detail = importMapperMapper.query_dingzhiDetailBynameId(category_id, list.get(i).getParentName());
                        if (dingzhi_detail != null) {
                            //执行修改
                            SmalltemplateTestInfo dingzhi = new SmalltemplateTestInfo();
                            dingzhi.setParameter(list.get(i).getParentName());
                            dingzhi.setNumbers(Double.parseDouble(list.get(i).getParentNumbers()));
                            dingzhi.setRemark(list.get(i).getParentRemark());
                            dingzhi.setSysOrgCode(loginuser.getOrgCode());
                            dingzhi.setUpdateBy(loginuser.getUsername());
                            dingzhi.setId(dingzhi_detail.getId());
                            //执行添加定制项
                            logicMapper.update_dingzhi(dingzhi);
                            System.out.println("==定制项修改=="+dingzhi_detail.getId());
                            //根据品类id和定制项id和参数名称查询参数
                            ExcelCategoryInfo canshu_detail = importMapperMapper.query_canshuDetailBynameId(category_id, dingzhi_detail.getId().toString(), list.get(i).getCategoryCode());
                            if (canshu_detail != null) {
                                //执行修改参数
                                MlccCategoryTestInfo update_canshu = new MlccCategoryTestInfo();
                                update_canshu.setCategoryName(list.get(i).getCategoryName());
                                update_canshu.setCategoryCode(list.get(i).getCategoryCode());
                                update_canshu.setImg(list.get(i).getImg());
                                update_canshu.setNumbers(list.get(i).getNumbers());
                                update_canshu.setSysOrgCode(loginuser.getOrgCode());
                                update_canshu.setUpdateBy(loginuser.getUsername());
                                update_canshu.setId(canshu_detail.getId());
                                logicMapper.update_dingzhi_canshu(update_canshu);
                                System.out.println("==参数修改=="+canshu_detail.getId()+"==="+list.get(i).getImg());
                            } else {
                                //执行循环添加定制项下的参数
                                MlccCategoryTestInfo add_canshu = new MlccCategoryTestInfo();
                                if(list.get(i).getCategoryCode()==null || "".equals(list.get(i).getCategoryCode()) || "".equals(list.get(i).getCategoryCode().trim())){
                                    Long startTs2 = System.currentTimeMillis(); // 当前时间戳
                                    //8位随机数
                                    int random2=new Random().nextInt(100)+1;//为变量赋随机值1-99999999;
                                    String category_code2 = "CS-"+startTs2.toString()+random2;
                                    add_canshu.setCategoryCode(category_code2);
                                }else {
                                    add_canshu.setCategoryCode(list.get(i).getCategoryCode());
                                }
                                add_canshu.setCategoryName(list.get(i).getCategoryName());
                                add_canshu.setParentId(dingzhi_detail.getId().toString());//定制项id
                                add_canshu.setImg(list.get(i).getImg());
                                add_canshu.setTopid(category_id);
                                add_canshu.setNumbers(list.get(i).getNumbers());
                                add_canshu.setIsType(0);
                                add_canshu.setSysOrgCode(loginuser.getOrgCode());
                                add_canshu.setCreateBy(loginuser.getUsername());
                                logicMapper.add_dingzhi_canshu(add_canshu);
                                System.out.println("==参数添加=="+"==="+list.get(i).getImg());
                            }
                        } else {
                            //循环添加定制项
                            Long startTs = System.currentTimeMillis(); // 当前时间戳
                            //8位随机数
                            int random = new Random().nextInt(1000) + 1;//为变量赋随机值1-99999999;
                            String category_code = "DZX-" + startTs.toString() + random;
                            SmalltemplateTestInfo dingzhi = new SmalltemplateTestInfo();
                            dingzhi.setModuleId("3");
                            dingzhi.setCategoryId(category_id);
                            dingzhi.setCode(category_code);
                            dingzhi.setParameter(list.get(i).getParentName());
                            dingzhi.setNumbers(Double.parseDouble(list.get(i).getParentNumbers()));
                            dingzhi.setIstype(0);
                            dingzhi.setRemark(list.get(i).getParentRemark());
                            dingzhi.setSysOrgCode(loginuser.getOrgCode());
                            dingzhi.setCreateBy(loginuser.getUsername());
                            //执行添加定制项
                            logicMapper.add_dingzhi(dingzhi);
                            System.out.println("==定制项添加=="+"==="+list.get(i).getParentName());
                            //根据品类id和定制项name查询定制项
                            ExcelCategoryInfo dingzhi_detail2 = importMapperMapper.query_dingzhiDetailBynameId(category_id, list.get(i).getParentName());
                            if (dingzhi_detail2 != null) {
                                //根据品类id和定制项id和参数名称查询参数
                                ExcelCategoryInfo canshu_detail = importMapperMapper.query_canshuDetailBynameId(category_id, dingzhi_detail2.getId().toString(), list.get(i).getCategoryCode());
                                if (canshu_detail != null) {
                                    //执行修改参数
                                    MlccCategoryTestInfo update_canshu = new MlccCategoryTestInfo();
                                    update_canshu.setCategoryName(list.get(i).getCategoryName());
                                    update_canshu.setCategoryCode(list.get(i).getCategoryCode());
                                    update_canshu.setImg(list.get(i).getImg());
                                    update_canshu.setNumbers(list.get(i).getNumbers());
                                    update_canshu.setSysOrgCode(loginuser.getOrgCode());
                                    update_canshu.setUpdateBy(loginuser.getUsername());
                                    update_canshu.setId(canshu_detail.getId());
                                    logicMapper.update_dingzhi_canshu(update_canshu);
                                    System.out.println("==参数修改22=="+"==="+list.get(i).getImg());
                                } else {
                                    //执行循环添加定制项下的参数
                                    MlccCategoryTestInfo add_canshu = new MlccCategoryTestInfo();
                                    if(list.get(i).getCategoryCode()==null || "".equals(list.get(i).getCategoryCode()) || "".equals(list.get(i).getCategoryCode().trim())){
                                        Long startTs2 = System.currentTimeMillis(); // 当前时间戳
                                        //8位随机数
                                        int random2=new Random().nextInt(100)+1;//为变量赋随机值1-99999999;
                                        String category_code2 = "CS-"+startTs2.toString()+random2;
                                        add_canshu.setCategoryCode(category_code2);
                                    }else {
                                        add_canshu.setCategoryCode(list.get(i).getCategoryCode());
                                    }
                                    add_canshu.setCategoryName(list.get(i).getCategoryName());
                                    add_canshu.setParentId(dingzhi_detail2.getId().toString());//定制项id
                                    add_canshu.setImg(list.get(i).getImg());
                                    add_canshu.setTopid(category_id);
                                    add_canshu.setNumbers(list.get(i).getNumbers());
                                    add_canshu.setIsType(0);
                                    add_canshu.setSysOrgCode(loginuser.getOrgCode());
                                    add_canshu.setCreateBy(loginuser.getUsername());
                                    logicMapper.add_dingzhi_canshu(add_canshu);
                                    System.out.println("==参数增加22=="+"==="+list.get(i).getCategoryName()+"=="+list.get(i).getImg());
                                }
                            }
                        }
                    }
                }
                return 0;
            }
        }catch (Exception e){
            System.out.println("导入数据失败=="+e.toString());
            return 2;
        }
    }
    /**
     * 导入绣字工艺--绣字部位
     * @param list    转换的实体类
     * @param category_id 类别id
     */
    @Override
    public int import_xiuzilocaltion(List<ExcelXiuziLocaltion> list, String category_id) {
        try {
            //获取登录用户信息
            LoginUser loginuser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            if (list == null || list.size() <= 0) {
                return 1;
            } else {
                //根据类别id和编码查询绣字信息
                for (int i=0;i<list.size();i++) {
                    if (list.get(i).getCode() != null && !"".equals(list.get(i).getCode()) && !"".equals(list.get(i).getCode().trim())) {
                        ExcelXiuziLocaltion excelXiuziLocaltion = importMapperMapper.query_xiuziLocaltionDetailBycodeId(category_id, list.get(i).getCode());
                        if (excelXiuziLocaltion != null) {
                            //执行修改
                            CategoryLocationTestInfo datainfo = new CategoryLocationTestInfo();
                            datainfo.setCode(list.get(i).getCode());
                            datainfo.setXiuziLocaltion(list.get(i).getXiuziLocaltion());
                            datainfo.setRemark(list.get(i).getRemark());
                            datainfo.setNumbers(list.get(i).getNumbers());
                            datainfo.setSysOrgCode(loginuser.getOrgCode());
                            datainfo.setUpdateBy(loginuser.getUsername());
                            datainfo.setId(excelXiuziLocaltion.getId());
                            logicMapper.update_xiuzi_location(datainfo);
                        } else {
                            //执行增加绣字部位
                            CategoryLocationTestInfo datainfo = new CategoryLocationTestInfo();
                            if(list.get(i).getCode()==null || "".equals(list.get(i).getCode()) || "".equals(list.get(i).getCode().trim())){
                                Long startTs2 = System.currentTimeMillis(); // 当前时间戳
                                //8位随机数
                                int random2=new Random().nextInt(100)+1;//为变量赋随机值1-99999999;
                                String category_code2 = "ZONE-"+startTs2.toString()+random2;
                                datainfo.setCode(category_code2);
                            }else {
                                datainfo.setCode(list.get(i).getCode());
                            }
                            datainfo.setXiuziLocaltion(list.get(i).getXiuziLocaltion());
                            datainfo.setCategoryId(category_id);
                            datainfo.setRemark(list.get(i).getRemark());
                            datainfo.setNumbers(list.get(i).getNumbers());
                            datainfo.setSysOrgCode(loginuser.getOrgCode());
                            datainfo.setCreateBy(loginuser.getUsername());
                            logicMapper.add_xiuzi_location(datainfo);
                        }
                    }
                }
                return 0;
            }
        }catch (Exception e){
            System.out.println("导入数据失败=="+e.toString());
            return 2;
        }
    }
    /**
     * 导入绣字工艺--绣字颜色
     * @param list    转换的实体类
     * @param category_id 类别id
     */
    @Override
    public int import_xiuzicolor(List<ExcelXiuziColor> list, String category_id) {
        try {
            //获取登录用户信息
            LoginUser loginuser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            if (list == null || list.size() <= 0) {
                return 1;
            } else {
                //根据类别id和编码查询绣字信息
                for (int i=0;i<list.size();i++) {
                    if (list.get(i).getCode() != null && !"".equals(list.get(i).getCode()) && !"".equals(list.get(i).getCode().trim())) {
                        ExcelXiuziColor excelXiuziLocaltion = importMapperMapper.query_xiuzicolorDetailBycodeId(category_id, list.get(i).getCode());
                        if (excelXiuziLocaltion != null) {
                            //执行修改
                            EmbroideredColorTestInfo datainfo = new EmbroideredColorTestInfo();
                            datainfo.setCode(list.get(i).getCode());
                            datainfo.setColorName(list.get(i).getColorName());
                            datainfo.setColorImg(list.get(i).getColorImg());
                            datainfo.setRemark(list.get(i).getRemark());
                            datainfo.setNumbers(list.get(i).getNumbers());
                            datainfo.setSysOrgCode(loginuser.getOrgCode());
                            datainfo.setUpdateBy(loginuser.getUsername());
                            datainfo.setId(excelXiuziLocaltion.getId());
                            logicMapper.update_xiuzi_color(datainfo);
                        } else {
                            //执行增加绣字部位
                            EmbroideredColorTestInfo datainfo = new EmbroideredColorTestInfo();
                            if(list.get(i).getCode()==null || "".equals(list.get(i).getCode()) || "".equals(list.get(i).getCode().trim())){
                                Long startTs2 = System.currentTimeMillis(); // 当前时间戳
                                //8位随机数
                                int random2=new Random().nextInt(100)+1;//为变量赋随机值1-99999999;
                                String category_code2 = "C-"+startTs2.toString()+random2;
                                datainfo.setCode(category_code2);
                            }else {
                                datainfo.setCode(list.get(i).getCode());
                            }
                            datainfo.setColorName(list.get(i).getColorName());
                            datainfo.setColorImg(list.get(i).getColorImg());
                            datainfo.setRemark(list.get(i).getRemark());
                            datainfo.setNumbers(list.get(i).getNumbers());
                            datainfo.setCategoryId(category_id);
                            datainfo.setSysOrgCode(loginuser.getOrgCode());
                            datainfo.setUpdateBy(loginuser.getUsername());
                            logicMapper.add_xiuzi_color(datainfo);
                        }
                    }
                }
                return 0;
            }
        }catch (Exception e){
            System.out.println("导入数据失败=="+e.toString());
            return 2;
        }
    }
    /**
     * 导入绣字工艺--绣字图片
     * @param list    转换的实体类
     * @param category_id 类别id
     */
    @Override
    public int import_xiuziimg(List<ExcelXiuziFontImg2> list, String category_id) {
        try {
            //获取登录用户信息
            LoginUser loginuser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            if (list == null || list.size() <= 0) {
                return 1;
            } else {
                //根据类别id和编码查询绣字信息
                for (int i=0;i<list.size();i++) {
                    if (list.get(i).getCode() != null && !"".equals(list.get(i).getCode()) && !"".equals(list.get(i).getCode().trim())) {
                        ExcelXiuziFontImg2 excelXiuziLocaltion = importMapperMapper.query_xiuziImgDetailBycodeId(category_id, list.get(i).getCode());
                        if (excelXiuziLocaltion != null) {
                            //执行修改
                            EmbroideredFontTestInfo datainfo = new EmbroideredFontTestInfo();
                            datainfo.setCode(list.get(i).getCode());
                            datainfo.setEmbroideredName(list.get(i).getEmbroideredName());
                            datainfo.setEmbroideredImage(list.get(i).getEmbroideredImage());
                            datainfo.setRemark(list.get(i).getRemark());
                            datainfo.setNumbers(list.get(i).getNumbers());
                            datainfo.setIsZitiImg(1);
                            datainfo.setSysOrgCode(loginuser.getOrgCode());
                            datainfo.setUpdateBy(loginuser.getUsername());
                            datainfo.setId(excelXiuziLocaltion.getId());
                            logicMapper.update_xiuzi_fontImg(datainfo);
                        } else {
                            //执行增加绣字部位
                            EmbroideredFontTestInfo datainfo = new EmbroideredFontTestInfo();
                            if(list.get(i).getCode()==null || "".equals(list.get(i).getCode()) || "".equals(list.get(i).getCode().trim())){
                                Long startTs2 = System.currentTimeMillis(); // 当前时间戳
                                //8位随机数
                                int random2=new Random().nextInt(100)+1;//为变量赋随机值1-99999999;
                                String category_code2 = "Y-"+startTs2.toString()+random2;
                                datainfo.setCode(category_code2);
                            }else {
                                datainfo.setCode(list.get(i).getCode());
                            }
                            datainfo.setEmbroideredName(list.get(i).getEmbroideredName());
                            datainfo.setEmbroideredImage(list.get(i).getEmbroideredImage());
                            datainfo.setRemark(list.get(i).getRemark());
                            datainfo.setNumbers(list.get(i).getNumbers());
                            datainfo.setIsZitiImg(1);
                            datainfo.setCategoryId(category_id);
                            datainfo.setSysOrgCode(loginuser.getOrgCode());
                            datainfo.setCreateBy(loginuser.getUsername());
                            logicMapper.add_xiuzi_fontImg(datainfo);
                        }
                    }
                }
                return 0;
            }
        }catch (Exception e){
            System.out.println("导入数据失败=="+e.toString());
            return 2;
        }
    }
    /**
     * 导入绣字工艺--绣字字体
     * @param list    转换的实体类
     * @param category_id 类别id
     */
    @Override
    public int import_xiuzifont(List<ExcelXiuziFontImg> list, String category_id) {
        try {
            //获取登录用户信息
            LoginUser loginuser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            if (list == null || list.size() <= 0) {
                return 1;
            } else {
                //根据类别id和编码查询绣字信息
                for (int i=0;i<list.size();i++) {
                    if (list.get(i).getCode() != null && !"".equals(list.get(i).getCode()) && !"".equals(list.get(i).getCode().trim())) {
                        ExcelXiuziFontImg excelXiuziLocaltion = importMapperMapper.query_xiuzifontDetailBycodeId(category_id, list.get(i).getCode());
                        if (excelXiuziLocaltion != null) {
                            //执行修改
                            EmbroideredFontTestInfo datainfo = new EmbroideredFontTestInfo();
                            datainfo.setCode(list.get(i).getCode());
                            datainfo.setEmbroideredName(list.get(i).getEmbroideredName());
                            datainfo.setEmbroideredImage(list.get(i).getEmbroideredImage());
                            datainfo.setRemark(list.get(i).getRemark());
                            datainfo.setNumbers(list.get(i).getNumbers());
                            datainfo.setIsZitiImg(0);
                            datainfo.setSysOrgCode(loginuser.getOrgCode());
                            datainfo.setUpdateBy(loginuser.getUsername());
                            datainfo.setId(excelXiuziLocaltion.getId());
                            logicMapper.update_xiuzi_fontImg(datainfo);
                        } else {
                            //执行增加绣字部位
                            EmbroideredFontTestInfo datainfo = new EmbroideredFontTestInfo();
                            if(list.get(i).getCode()==null || "".equals(list.get(i).getCode()) || "".equals(list.get(i).getCode().trim())){
                                Long startTs2 = System.currentTimeMillis(); // 当前时间戳
                                //8位随机数
                                int random2=new Random().nextInt(100)+1;//为变量赋随机值1-99999999;
                                String category_code2 = "T-"+startTs2.toString()+random2;
                                datainfo.setCode(category_code2);
                            }else {
                                datainfo.setCode(list.get(i).getCode());
                            }
                            datainfo.setEmbroideredName(list.get(i).getEmbroideredName());
                            datainfo.setEmbroideredImage(list.get(i).getEmbroideredImage());
                            datainfo.setRemark(list.get(i).getRemark());
                            datainfo.setNumbers(list.get(i).getNumbers());
                            datainfo.setIsZitiImg(0);
                            datainfo.setCategoryId(category_id);
                            datainfo.setSysOrgCode(loginuser.getOrgCode());
                            datainfo.setCreateBy(loginuser.getUsername());
                            logicMapper.add_xiuzi_fontImg(datainfo);
                        }
                    }
                }
                return 0;
            }
        }catch (Exception e){
            System.out.println("导入数据失败=="+e.toString());
            return 2;
        }
    }
    /**
     * 导入绣字工艺--绣字高度
     * @param list    转换的实体类
     * @param category_id 类别id
     */
    @Override
    public int import_xiuziheight(List<ExcelXiuziHeight> list, String category_id) {
        try {
            //获取登录用户信息
            LoginUser loginuser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            if (list == null || list.size() <= 0) {
                return 1;
            } else {
                //根据类别id和编码查询绣字信息
                for (int i=0;i<list.size();i++) {
                    if (list.get(i).getHeightCode() != null && !"".equals(list.get(i).getHeightCode()) && !"".equals(list.get(i).getHeightCode().trim())) {
                        ExcelXiuziHeight excelXiuziLocaltion = importMapperMapper.query_xiuziheightDetailBycodeId(category_id, list.get(i).getHeightCode());
                        if (excelXiuziLocaltion != null) {
                            //执行修改
                            EmbroidedHeightInfo datainfo = new EmbroidedHeightInfo();
                            datainfo.setEmbroideredHeight(list.get(i).getEmbroideredHeight());
                            datainfo.setHeightCode(list.get(i).getHeightCode());
                            datainfo.setRemark(list.get(i).getRemark());
                            datainfo.setSysOrgCode(loginuser.getOrgCode());
                            datainfo.setUpdateBy(loginuser.getUsername());
                            datainfo.setId(excelXiuziLocaltion.getId());
                            logicMapper.update_xiuzi_height(datainfo);
                        } else {
                            //循环添加绣字高度
                            EmbroidedHeightInfo datainfo = new EmbroidedHeightInfo();
                            if(list.get(i).getHeightCode()==null || "".equals(list.get(i).getHeightCode()) || "".equals(list.get(i).getHeightCode().trim())){
                                Long startTs2 = System.currentTimeMillis(); // 当前时间戳
                                //8位随机数
                                int random2=new Random().nextInt(100)+1;//为变量赋随机值1-99999999;
                                String category_code2 = "HEIGHT"+startTs2.toString()+random2;
                                datainfo.setHeightCode(category_code2);
                            }else {
                                datainfo.setHeightCode(list.get(i).getHeightCode());
                            }
                            datainfo.setEmbroideredHeight(list.get(i).getEmbroideredHeight());
                            datainfo.setCategoryId(category_id);
                            datainfo.setRemark(list.get(i).getRemark());
                            datainfo.setSysOrgCode(loginuser.getOrgCode());
                            datainfo.setCreateBy(loginuser.getUsername());
                            logicMapper.add_xiuzi_height(datainfo);
                        }
                    }
                }
                return 0;
            }
        }catch (Exception e){
            System.out.println("导入数据失败=="+e.toString());
            return 2;
        }
    }
    /**
     * 导入尺寸--成衣
     * @param list    转换的实体类
     * @param category_id 类别id
     */
    @Override
    public int import_bodysizes_chengyi(List<ExcelBodysizes> list, String category_id) {
        try {
            //获取登录用户信息
            LoginUser loginuser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            if (list == null || list.size() <= 0) {
                return 1;
            } else {
                //根据类别id和名称查询尺寸信息
                for (int i=0;i<list.size();i++) {
                    if (list.get(i).getCode() != null && !"".equals(list.get(i).getCode()) && !"".equals(list.get(i).getCode().trim())) {
                        ExcelBodysizes excelXiuziLocaltion = importMapperMapper.query_bodysizeDetailBynameId(category_id, list.get(i).getCode(), 0);
                        if (excelXiuziLocaltion != null) {
                            //执行修改
                            BodySizeTestInfo datainfo = new BodySizeTestInfo();
                            datainfo.setBodySizename(list.get(i).getBodySizename());
                            datainfo.setUnit(list.get(i).getUnit());
                            datainfo.setModuleId("5");
                            datainfo.setNumbers(list.get(i).getNumbers());
                            datainfo.setIsJin(0);
                            datainfo.setMax(list.get(i).getMax());
                            datainfo.setMin(list.get(i).getMin());
                            datainfo.setIsBitian(list.get(i).getIsBitian());
                            datainfo.setSysOrgCode(loginuser.getOrgCode());
                            datainfo.setUpdateBy(loginuser.getUsername());
                            datainfo.setId(excelXiuziLocaltion.getId());
                            logicMapper.update_testbodySize(datainfo);
                        } else {
                            //循环添加身体尺寸
                            BodySizeTestInfo datainfo = new BodySizeTestInfo();
                            if(list.get(i).getCode()==null || "".equals(list.get(i).getCode()) || "".equals(list.get(i).getCode().trim())){
                                Long startTs2 = System.currentTimeMillis(); // 当前时间戳
                                //8位随机数
                                int random2=new Random().nextInt(100)+1;//为变量赋随机值1-99999999;
                                String category_code2 = "BSZIE-"+startTs2.toString()+random2;
                                datainfo.setCode(category_code2);
                            }else {
                                datainfo.setCode(list.get(i).getCode());
                            }
                            datainfo.setBodySizename(list.get(i).getBodySizename());
                            datainfo.setUnit(list.get(i).getUnit());
                            datainfo.setModuleId("5");
                            datainfo.setCategoryId(category_id);
                            datainfo.setNumbers(list.get(i).getNumbers());
                            datainfo.setIsJin(0);
                            datainfo.setMax(list.get(i).getMax());
                            datainfo.setMin(list.get(i).getMin());
                            datainfo.setIsBitian(list.get(i).getIsBitian());
                            datainfo.setSysOrgCode(loginuser.getOrgCode());
                            datainfo.setCreateBy(loginuser.getUsername());
                            logicMapper.add_testbodySize(datainfo);
                        }
                    }
                    else {//编号为空--判断名称
                        if(list.get(i).getBodySizename() != null && !"".equals(list.get(i).getBodySizename()) && !"".equals(list.get(i).getBodySizename().trim())){
                            ExcelBodysizes excelXiuziLocaltion = importMapperMapper.query_bodysizeDetailByname(category_id, list.get(i).getBodySizename(), 0);
                            if(excelXiuziLocaltion==null){
                                Long startTs2 = System.currentTimeMillis(); // 当前时间戳
                                //8位随机数
                                int random2=new Random().nextInt(100)+1;//为变量赋随机值1-99999999;
                                String category_code2 = "BSZIE-"+startTs2.toString()+random2;
                                //循环添加身体尺寸
                                BodySizeTestInfo datainfo = new BodySizeTestInfo();
                                datainfo.setCode(category_code2);
                                datainfo.setBodySizename(list.get(i).getBodySizename());
                                datainfo.setUnit(list.get(i).getUnit());
                                datainfo.setModuleId("5");
                                datainfo.setCategoryId(category_id);
                                datainfo.setNumbers(list.get(i).getNumbers());
                                datainfo.setIsJin(0);
                                datainfo.setMax(list.get(i).getMax());
                                datainfo.setMin(list.get(i).getMin());
                                datainfo.setIsBitian(list.get(i).getIsBitian());
                                datainfo.setSysOrgCode(loginuser.getOrgCode());
                                datainfo.setCreateBy(loginuser.getUsername());
                                logicMapper.add_testbodySize(datainfo);
                            }
                        }
                    }
                }
                return 0;
            }
        }catch (Exception e){
            System.out.println("导入数据失败=="+e.toString());
            return 2;
        }
    }
    /**
     * 导入尺寸--净体
     * @param list    转换的实体类
     * @param category_id 类别id
     */
    @Override
    public int import_bodysizes_jingyi(List<ExcelBodysizes> list, String category_id) {
        try {
            //获取登录用户信息
            LoginUser loginuser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            if (list == null || list.size() <= 0) {
                return 1;
            } else {
                //根据类别id和名称查询尺寸信息
                for (int i=0;i<list.size();i++) {
                    if (list.get(i).getCode() != null && !"".equals(list.get(i).getCode()) && !"".equals(list.get(i).getCode().trim())) {
                        ExcelBodysizes excelXiuziLocaltion = importMapperMapper.query_bodysizeDetailBynameId(category_id, list.get(i).getCode(), 1);
                        if (excelXiuziLocaltion != null) {
                            //执行修改
                            BodySizeTestInfo datainfo = new BodySizeTestInfo();
                            datainfo.setBodySizename(list.get(i).getBodySizename());
                            datainfo.setUnit(list.get(i).getUnit());
                            datainfo.setModuleId("5");
                            datainfo.setNumbers(list.get(i).getNumbers());
                            datainfo.setIsJin(1);
                            datainfo.setMax(list.get(i).getMax());
                            datainfo.setMin(list.get(i).getMin());
                            datainfo.setIsBitian(list.get(i).getIsBitian());
                            datainfo.setSysOrgCode(loginuser.getOrgCode());
                            datainfo.setUpdateBy(loginuser.getUsername());
                            datainfo.setId(excelXiuziLocaltion.getId());
                            logicMapper.update_testbodySize(datainfo);
                        } else {
                            //循环添加身体尺寸
                            BodySizeTestInfo datainfo = new BodySizeTestInfo();
                            if(list.get(i).getCode()==null || "".equals(list.get(i).getCode()) || "".equals(list.get(i).getCode().trim())){
                                Long startTs2 = System.currentTimeMillis(); // 当前时间戳
                                //8位随机数
                                int random2=new Random().nextInt(100)+1;//为变量赋随机值1-99999999;
                                String category_code2 = "JSZIE-"+startTs2.toString()+random2;
                                datainfo.setCode(category_code2);
                            }else {
                                datainfo.setCode(list.get(i).getCode());
                            }
                            datainfo.setBodySizename(list.get(i).getBodySizename());
                            datainfo.setUnit(list.get(i).getUnit());
                            datainfo.setModuleId("5");
                            datainfo.setCategoryId(category_id);
                            datainfo.setNumbers(list.get(i).getNumbers());
                            datainfo.setIsJin(1);
                            datainfo.setMax(list.get(i).getMax());
                            datainfo.setMin(list.get(i).getMin());
                            datainfo.setIsBitian(list.get(i).getIsBitian());
                            datainfo.setSysOrgCode(loginuser.getOrgCode());
                            datainfo.setCreateBy(loginuser.getUsername());
                            logicMapper.add_testbodySize(datainfo);
                        }
                    } else {//编号为空--判断名称
                        if(list.get(i).getBodySizename() != null && !"".equals(list.get(i).getBodySizename()) && !"".equals(list.get(i).getBodySizename().trim())){
                            ExcelBodysizes excelXiuziLocaltion = importMapperMapper.query_bodysizeDetailByname(category_id, list.get(i).getBodySizename(), 1);
                            if(excelXiuziLocaltion==null){
                                Long startTs2 = System.currentTimeMillis(); // 当前时间戳
                                //8位随机数
                                int random2=new Random().nextInt(100)+1;//为变量赋随机值1-99999999;
                                String category_code2 = "JSZIE-"+startTs2.toString()+random2;
                                //循环添加身体尺寸
                                BodySizeTestInfo datainfo = new BodySizeTestInfo();
                                datainfo.setCode(category_code2);
                                datainfo.setBodySizename(list.get(i).getBodySizename());
                                datainfo.setUnit(list.get(i).getUnit());
                                datainfo.setModuleId("5");
                                datainfo.setCategoryId(category_id);
                                datainfo.setNumbers(list.get(i).getNumbers());
                                datainfo.setIsJin(1);
                                datainfo.setMax(list.get(i).getMax());
                                datainfo.setMin(list.get(i).getMin());
                                datainfo.setIsBitian(list.get(i).getIsBitian());
                                datainfo.setSysOrgCode(loginuser.getOrgCode());
                                datainfo.setCreateBy(loginuser.getUsername());
                                logicMapper.add_testbodySize(datainfo);
                            }
                        }
                    }
                }
                return 0;
            }
        }catch (Exception e){
            System.out.println("导入数据失败=="+e.toString());
            return 2;
        }
    }
    /**
     * 导入返修尺寸
     * @param list    转换的实体类
     * @param category_id 类别id
     */
    @Override
    public int import_fanxiu_bodysizes(List<ExcelFanxiuBodysize> list, String category_id) {
        try {
            //获取登录用户信息
            LoginUser loginuser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            if (list == null || list.size() <= 0) {
                return 1;
            } else {
                //根据类别id和名称查询尺寸信息
                for (int i=0;i<list.size();i++) {
                    if (list.get(i).getCode() != null && !"".equals(list.get(i).getCode()) && !"".equals(list.get(i).getCode().trim())) {
                        ExcelFanxiuBodysize excelXiuziLocaltion = importMapperMapper.query_fanxiubodysizeDetailBynameId(category_id, list.get(i).getCode());
                        if (excelXiuziLocaltion != null) {
                            //执行修改
                            CategoryFanxiuTestInfo datainfo = new CategoryFanxiuTestInfo();
                            datainfo.setBodySizename(list.get(i).getBodySizename());
                            datainfo.setUnit(list.get(i).getUnit());
                            datainfo.setNumbers(list.get(i).getNumbers());
                            datainfo.setMax(list.get(i).getMax());
                            datainfo.setMin(list.get(i).getMin());
                            datainfo.setRemark(list.get(i).getRemark());
                            datainfo.setSysOrgCode(loginuser.getOrgCode());
                            datainfo.setCreateBy(loginuser.getUsername());
                            datainfo.setId(excelXiuziLocaltion.getId());
                            logicMapper.update_testFanxiuSize(datainfo);
                        } else {
                            CategoryFanxiuTestInfo datainfo = new CategoryFanxiuTestInfo();
                            if(list.get(i).getCode()==null || "".equals(list.get(i).getCode()) || "".equals(list.get(i).getCode().trim())){
                                Long startTs2 = System.currentTimeMillis(); // 当前时间戳
                                //8位随机数
                                int random2=new Random().nextInt(100)+1;//为变量赋随机值1-99999999;
                                String category_code2 = "FX-"+startTs2.toString()+random2;
                                datainfo.setCode(category_code2);
                            }else {
                                datainfo.setCode(list.get(i).getCode());
                            }
                            datainfo.setBodySizename(list.get(i).getBodySizename());
                            datainfo.setUnit(list.get(i).getUnit());
                            datainfo.setCategoryId(category_id);
                            datainfo.setNumbers(list.get(i).getNumbers());
                            datainfo.setMax(list.get(i).getMax());
                            datainfo.setMin(list.get(i).getMin());
                            datainfo.setRemark(list.get(i).getRemark());
                            datainfo.setSysOrgCode(loginuser.getOrgCode());
                            datainfo.setCreateBy(loginuser.getUsername());
                            logicMapper.add_testFanxiuSize(datainfo);
                        }
                    }else {
                        if(list.get(i).getBodySizename() != null && !"".equals(list.get(i).getBodySizename()) && !"".equals(list.get(i).getBodySizename().trim())){
                            ExcelFanxiuBodysize excelXiuziLocaltion = importMapperMapper.query_fanxiubodysizeDetailByname(category_id, list.get(i).getBodySizename());
                            if(excelXiuziLocaltion==null){
                                CategoryFanxiuTestInfo datainfo = new CategoryFanxiuTestInfo();
                                Long startTs2 = System.currentTimeMillis(); // 当前时间戳
                                //8位随机数
                                int random2=new Random().nextInt(100)+1;//为变量赋随机值1-99999999;
                                String category_code2 = "FX-"+startTs2.toString()+random2;
                                datainfo.setCode(category_code2);
                                datainfo.setBodySizename(list.get(i).getBodySizename());
                                datainfo.setUnit(list.get(i).getUnit());
                                datainfo.setCategoryId(category_id);
                                datainfo.setNumbers(list.get(i).getNumbers());
                                datainfo.setMax(list.get(i).getMax());
                                datainfo.setMin(list.get(i).getMin());
                                datainfo.setRemark(list.get(i).getRemark());
                                datainfo.setSysOrgCode(loginuser.getOrgCode());
                                datainfo.setCreateBy(loginuser.getUsername());
                                logicMapper.add_testFanxiuSize(datainfo);
                            }
                        }
                    }
                }
                return 0;
            }
        }catch (Exception e){
            System.out.println("导入数据失败=="+e.toString());
            return 2;
        }
    }

    /**
     * 导入工艺类型
     * @param list    上传的Excel文件
     * @param category_id 品类id
     */
    @Override
    public int import_category_gongyi(List<ExcelCategoryGongyi> list, String category_id) {
        try {
            //获取登录用户信息
            LoginUser loginuser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            if (list == null || list.size() <= 0) {
                return 1;
            } else {
                //根据类别id和编码查询绣字信息--编码一换名称
                for (int i=0;i<list.size();i++) {
                    if (list.get(i).getCategoryName() != null && !"".equals(list.get(i).getCategoryName()) && !"".equals(list.get(i).getCategoryName().trim())) {
                        ExcelCategoryGongyi excelXiuziLocaltion = importMapperMapper.query_gongyiDetailBycodeId(category_id, list.get(i).getCategoryName());
                        if (excelXiuziLocaltion != null) {
                            //循环修改工艺
                            MlccCategoryTestInfo datainfo = new MlccCategoryTestInfo();
                            datainfo.setCategoryName(list.get(i).getCategoryName());
                            datainfo.setImg(list.get(i).getImg());
                            datainfo.setRemark(list.get(i).getRemark());
                            datainfo.setCategorySex(0);
                            datainfo.setNumbers(list.get(i).getNumbers());
                            datainfo.setSysOrgCode(loginuser.getOrgCode());
                            datainfo.setUpdateBy(loginuser.getUsername());
                            datainfo.setId(excelXiuziLocaltion.getId());
                            datainfo.setTopid(category_id);
                            logicMapper.update_category_pingleiName(datainfo);
                        } else {
                            //循环添加工艺
                            MlccCategoryTestInfo datainfo = new MlccCategoryTestInfo();
                            Long startTs2 = System.currentTimeMillis(); // 当前时间戳
                            //8位随机数
                            int random2=new Random().nextInt(1000000)+1;//为变量赋随机值1-99999999;
                            String category_code2 = "GY-"+startTs2.toString()+random2;
                            datainfo.setCategoryCode(category_code2);//赋值编号
                            datainfo.setCategoryName(list.get(i).getCategoryName());
                            datainfo.setParentId("4");
                            datainfo.setImg(list.get(i).getImg());
                            datainfo.setRemark(list.get(i).getRemark());
                            datainfo.setTopid(category_id);
                            datainfo.setIsType(4);
                            datainfo.setCategorySex(0);
                            datainfo.setNumbers(list.get(i).getNumbers());
                            datainfo.setSysOrgCode(loginuser.getOrgCode());
                            datainfo.setCreateBy(loginuser.getUsername());
                            logicMapper.add_category_pinglei(datainfo);
                            MlccCategoryTestInfo testInfo = logicMapper.select_testcategoryDetailBycode(category_code2);
                            if(testInfo!=null){
                                logicMapper.update_gongyicode_code("GY-0"+testInfo.getId(),category_code2);
                            }
                        }
                    }else {//编号为空--判断名称
//                        if (list.get(i).getCategoryName() != null && !"".equals(list.get(i).getCategoryName()) && !"".equals(list.get(i).getCategoryName().trim())) {
//                            ExcelCategoryGongyi excelXiuziLocaltion = importMapperMapper.query_gongyiDetailByname(category_id, list.get(i).getCategoryName());
//                            if (excelXiuziLocaltion == null) {
//                                Long startTs2 = System.currentTimeMillis(); // 当前时间戳
//                                //8位随机数
//                                int random2=new Random().nextInt(100)+1;//为变量赋随机值1-99999999;
//                                String category_code2 = "GY-"+startTs2.toString()+random2;
//                                //循环添加工艺
//                                MlccCategoryTestInfo datainfo = new MlccCategoryTestInfo();
//                                datainfo.setCategoryCode(category_code2);
//                                datainfo.setCategoryName(list.get(i).getCategoryName());
//                                datainfo.setParentId("4");
//                                datainfo.setImg(list.get(i).getImg());
//                                datainfo.setRemark(list.get(i).getRemark());
//                                datainfo.setTopid(category_id);
//                                datainfo.setIsType(4);
//                                datainfo.setCategorySex(0);
//                                datainfo.setNumbers(list.get(i).getNumbers());
//                                datainfo.setSysOrgCode(loginuser.getOrgCode());
//                                datainfo.setCreateBy(loginuser.getUsername());
//                                logicMapper.add_category_pinglei(datainfo);
//                            }
//                        }
                    }
                }
                return 0;
            }
        }catch (Exception e){
            System.out.println("导入数据失败=="+e.toString());
            return 2;
        }
    }

    /**
     * 导入撞色数据
     * @param list    转换的实体类
     * @param category_id 类别id
     */
    @Override
    public int import_category_zhuangse(List<ExcelZhuangseInfo> list, String category_id) {
//        try {
            //获取登录用户信息
            LoginUser loginuser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            if (list == null || list.size() <= 0) {
                return 1;
            } else {
                //根据类别id和编码查询撞色信息
                for (int i=0;i<list.size();i++){
                    ExcelZhuangseInfo excelXiuziLocaltion = importMapperMapper.query_zhuangseDetailBycodeId(category_id,list.get(i).getCode());
                    if(excelXiuziLocaltion!=null){
                        String dingzhiId="";
                        String canshuId="";
                        //根据名称和品类id查询定制项得到定制项id
                        ExcelCategoryInfo dingzhi_detail = importMapperMapper.query_dingzhiDetailBynameId(category_id, list.get(i).getDingzhiName());
                        //根据名称和品类id查询定制项下的参数得到参数id
                        if(dingzhi_detail!=null){
                            dingzhiId = dingzhi_detail.getId().toString();
                            ExcelCategoryInfo canshu_detail = importMapperMapper.query_canshuDetailByname(category_id, dingzhi_detail.getId().toString(), list.get(i).getCategoryName());
                            if(canshu_detail!=null){
                                canshuId = canshu_detail.getId().toString();
                            }
                        }
                        //循环修改工艺
                        ContrastcolorTestInfo datainfo = new ContrastcolorTestInfo();
                        datainfo.setCode(list.get(i).getCode());
                        datainfo.setContrastColorName(list.get(i).getContrastColorName());
                        datainfo.setContrastColorImg(list.get(i).getContrastColorImg());
                        datainfo.setCategoryId(canshuId);
                        datainfo.setDingzhiId(dingzhiId);
                        datainfo.setRemark(list.get(i).getRemark());
                        datainfo.setSysOrgCode(loginuser.getOrgCode());
                        datainfo.setUpdateBy(loginuser.getUsername());
                        datainfo.setId(excelXiuziLocaltion.getId());
                        logicMapper.update_zhuangse(datainfo);
                    }else {
                        String dingzhiId="";
                        String canshuId="";
                        //根据名称和品类id查询定制项得到定制项id
                        ExcelCategoryInfo dingzhi_detail = importMapperMapper.query_dingzhiDetailBynameId(category_id, list.get(i).getDingzhiName());
                        //根据名称和品类id查询定制项下的参数得到参数id
                        if(dingzhi_detail!=null){
                            dingzhiId = dingzhi_detail.getId().toString();
                            ExcelCategoryInfo canshu_detail = importMapperMapper.query_canshuDetailByname(category_id, dingzhi_detail.getId().toString(), list.get(i).getCategoryName());
                            if(canshu_detail!=null){
                                canshuId = canshu_detail.getId().toString();
                            }
                        }
                        //循环添加撞色
                        ContrastcolorTestInfo datainfo = new ContrastcolorTestInfo();
                        if(list.get(i).getCode()==null || "".equals(list.get(i).getCode()) || "".equals(list.get(i).getCode().trim())){
                            Long startTs2 = System.currentTimeMillis(); // 当前时间戳
                            //8位随机数
                            int random2=new Random().nextInt(100)+1;//为变量赋随机值1-99999999;
                            String category_code2 = "ZS-"+startTs2.toString()+random2;
                            datainfo.setCode(category_code2);
                        }else {
                            datainfo.setCode(list.get(i).getCode());
                        }
                        datainfo.setTopId(category_id);
                        datainfo.setContrastColorName(list.get(i).getContrastColorName());
                        datainfo.setContrastColorImg(list.get(i).getContrastColorImg());
                        datainfo.setCategoryId(canshuId);
                        datainfo.setDingzhiId(dingzhiId);
                        datainfo.setRemark(list.get(i).getRemark());
                        datainfo.setSysOrgCode(loginuser.getOrgCode());
                        datainfo.setCreateBy(loginuser.getUsername());
                        logicMapper.add_zhuangse(datainfo);
                    }
                }
                return 0;
            }
//        }catch (Exception e){
//            System.out.println("导入数据失败=="+e.toString());
//            return 2;
//        }
    }

    /**
     * 一键清空有关定制项的数据
     * @param category_id 类别id
     */
    @Override
    public int clean_all_categorydata(String category_id) {
        try {
           importMapperMapper.real_delete_dingzhi(category_id);
           importMapperMapper.real_delete_dingzhi_canshu(category_id);
           importMapperMapper.real_delete_contrastcolor(category_id,"0");
           importMapperMapper.real_delete_MutexTestInfo(category_id,"0");
           return 0;
        }catch (Exception e){
            System.out.println("删除失败="+e.toString());
            return 1;
        }
    }

}
